use inequality_data_for_mike_murto , clear

set more off

keep if ref_year>1982 

*drop years for which we do not have all observations
drop if ref_year==1979
drop if ref_year==2013

*ensure summation in Total Vehicle Cons works correctly
foreach var of varlist vehicpq vehiccq cartknpq cartkncq cartkupq cartkucq caropq carocq {
replace `var' = 0 if (`var'==. & ref_year>=1980) 
}

*Generate component totals to create different measures of consumption
* food at home
gen fdhome_tot = .
replace fdhome_tot = fdhome/4 if (ref_year<1970)
replace fdhome_tot = d_fdhome/4 if (ref_year>=1970 & ref_year<=1979) 
replace fdhome_tot = fdhomepq2+fdhomecq2 if (ref_year >= 1980)
*transportation 
gen trans_tot = .
replace trans_tot = (publictransother+automobileops+n_tot_vflow1)/4 if(ref_year<1970) 
replace trans_tot = (d_gas + d_othvehic-d_autofin+d_pubtrans+n_tot_vflow1)/4 if (ref_year>=1970 & ref_year<=1979)
replace trans_tot = (transpq+transcq+n_tot_vflow1-(vehfinpq+vehfincq+vehicpq+vehiccq+cartknpq+cartkncq+cartkupq+cartkucq+caropq+carocq)) if (ref_year>=1980)
*utilities 
gen util_tot = .
replace util_tot = utilites/4 if (ref_year<1970) 
replace util_tot = d_utility/4 if (ref_year>=1970 & ref_year<=1979) 
replace util_tot = utilpq+utilcq if (ref_year>=1980) 
*Adjusted utilities from predictions
*replace util_tot = n_adj_util_floor if (cutenure==4 & _merge_rnt==3)
*housing
gen hous_tot = . 
replace hous_tot = (totrentdwe +hflow)/4 if (ref_year<1970) 
replace hous_tot = (d_rent + hflow)/4 if (ref_year>=1970 & ref_year<=1979) 
replace hous_tot = rendwepq+rendwecq+hflow+tflow2-tflow if (ref_year>=1980) 
*difference in adjusted and reported utilities is utility payment included in rent (forced to be positive) 
*gen diff_rnt = n_adj_util_floor - (utilpq+utilcq) if (cutenure==4 & _merge_rnt==3) 
*replace hous_tot = hous_tot - diff_rnt if (cutenure==4 & _merge_rnt==3 & diff_rnt>0 & diff_rnt!=.) 
 
*Recode cons5 to drop all elements of transportation that are not well-measured
rename cons5 uncorrected_cons
gen double cons5 = .
replace cons5= ((uncorrected_cons*scale)+(n_tot_vflow1/4)+gas_mo-trans_tot)/scale if (ref_year<1980)
replace cons5=((uncorrected_cons*scale)+n_tot_vflow1+gas_mo-trans_tot)/scale if (ref_year>=1980)
replace cons5= ((cons5*scale)-(tflow2-tflow))/scale if (ref_year>=1980) 

*Generate core less food, non-housing core, and core less food and utilities 
gen double cons10 = ((cons5*scale)-fdhome_tot)/scale 
gen double cons11 = ((cons5*scale)-hous_tot)/scale 
gen double cons12 = ((cons10*scale)-util_tot)/scale
gen double cons13 = ((cons5*scale)-gas_mo)/scale
gen double cons14 = ((cons5*scale)-(n_tot_vflow1/4))/scale if (ref_year<1980)
replace cons14 = ((cons5*scale)-n_tot_vflow1)/scale if (ref_year>=1980)

*Generate cpi defalator
gen cpi_u_rs_adj_11=.
replace cpi_u_rs_adj_11=4.263 if (ref_year==1960)
replace cpi_u_rs_adj_11=4.266 if (ref_year==1961)
replace cpi_u_rs_adj_11=4.27 if (ref_year==1962)
replace cpi_u_rs_adj_11=4.259 if (ref_year==1963)
replace cpi_u_rs_adj_11=4.248 if (ref_year==1964)
replace cpi_u_rs_adj_11=4.228 if (ref_year==1965)
replace cpi_u_rs_adj_11=4.156 if (ref_year==1966)
replace cpi_u_rs_adj_11=4.075 if (ref_year==1967)
replace cpi_u_rs_adj_11=3.962 if (ref_year==1968)
replace cpi_u_rs_adj_11=3.834 if (ref_year==1969)
replace cpi_u_rs_adj_11=3.693 if (ref_year==1970)
replace cpi_u_rs_adj_11=3.575 if (ref_year==1971)
replace cpi_u_rs_adj_11=3.509 if (ref_year==1972)
replace cpi_u_rs_adj_11=3.337 if (ref_year==1973)
replace cpi_u_rs_adj_11=3.064 if (ref_year==1974)
replace cpi_u_rs_adj_11=2.86 if (ref_year==1975)
replace cpi_u_rs_adj_11=2.733 if (ref_year==1976) 
replace cpi_u_rs_adj_11=2.597 if (ref_year==1977)
replace cpi_u_rs_adj_11=2.449 if (ref_year==1978)
replace cpi_u_rs_adj_11=2.252 if (ref_year==1979)
replace cpi_u_rs_adj_11=2.041 if (ref_year==1980) 
replace cpi_u_rs_adj_11=1.878 if (ref_year==1981)
replace cpi_u_rs_adj_11=1.784 if (ref_year==1982)
replace cpi_u_rs_adj_11=1.724 if (ref_year==1983)
replace cpi_u_rs_adj_11=1.669 if (ref_year==1984)
replace cpi_u_rs_adj_11=1.627 if (ref_year==1985)
replace cpi_u_rs_adj_11=1.61 if (ref_year==1986)
replace cpi_u_rs_adj_11=1.57 if (ref_year==1987) 
replace cpi_u_rs_adj_11=1.526 if (ref_year==1988)
replace cpi_u_rs_adj_11=1.474 if (ref_year==1989)
replace cpi_u_rs_adj_11=1.415 if (ref_year==1990)
replace cpi_u_rs_adj_11=1.377 if (ref_year==1991)
replace cpi_u_rs_adj_11=1.353 if (ref_year==1992)
replace cpi_u_rs_adj_11=1.331 if (ref_year==1993)
replace cpi_u_rs_adj_11=1.313 if (ref_year==1994)
replace cpi_u_rs_adj_11=1.293 if (ref_year==1995)
replace cpi_u_rs_adj_11=1.269 if (ref_year==1996)
replace cpi_u_rs_adj_11=1.252 if (ref_year==1997)
replace cpi_u_rs_adj_11=1.245 if (ref_year==1998)
replace cpi_u_rs_adj_11=1.229 if (ref_year==1999) 
replace cpi_u_rs_adj_11=1.198 if (ref_year==2000)
replace cpi_u_rs_adj_11=1.175 if (ref_year==2001)
replace cpi_u_rs_adj_11=1.165 if (ref_year==2002)
replace cpi_u_rs_adj_11=1.149 if (ref_year==2003)
replace cpi_u_rs_adj_11=1.127 if (ref_year==2004)
replace cpi_u_rs_adj_11=1.099 if (ref_year==2005)
replace cpi_u_rs_adj_11=1.073 if (ref_year==2006)
replace cpi_u_rs_adj_11=1.051 if (ref_year==2007)
replace cpi_u_rs_adj_11=1.02 if (ref_year==2008) 
replace cpi_u_rs_adj_11=1.032 if (ref_year==2009) 
replace cpi_u_rs_adj_11=1.024 if (ref_year==2010) 
replace cpi_u_rs_adj_11=1 if (ref_year==2011)
replace cpi_u_rs_adj_11=.987 if (ref_year==2012)

*Generate real consumption figures
foreach var of varlist cons* {
gen r_`var' = `var'*cpi_u_rs_adj_11
}

label var r_cons5 "Well measured consumption (core)" 
label var r_cons6 "Total Consumption"
label var r_cons10 "Core less food" 
label var r_cons11 "Non-housing core"
label var r_cons12 "Core less food and utilities"
label var r_cons13 "Core less gas and motor oil"
*create binary variable for gender
gen male=2-sex_ref
*Gen family type dummies
forvalues x=1/5 {
gen fam`x'=0
replace fam`x'=1 if ftype==`x'
gen fam_male`x'=fam`x'*male
}
*Create region dummies 
forvalues x=1/4 {
gen reg`x' =0
replace reg`x'=1 if region==`x'
replace reg`x'=0 if bls_urbn==2
}
gen urban=2-bls_urbn
*Create race dummies
gen race1=0
replace race1=1 if (ref_race==1 & hispanic==0)
gen race2=0
replace race2=1 if (ref_race==2 & hispanic==0)
gen race3=1-race1-race2
*Gen employment indicators 
gen emp=0 if (incweek1 != .)
replace emp=1 if (incweek1 !=. & incweek1>0)
gen emp2=0 if (incweek2 != .) 
replace emp2=1 if (incweek2 != . & incweek2>0)
gen emp_type=0
replace emp_type=1 if (emp==1 & emp2==1)
replace emp_type=2 if (emp==1 & emp2==0)
replace emp_type=3 if (emp==0 & emp2==1)
replace emp_type=4 if (emp==0 & emp2==0)
*Gen race indicator  
gen race_type=. 
replace race_type=1 if (race1==1)
replace race_type=2 if (race2==1)
replace race_type=3 if (race3==1)
*Gen education indicators and dummies (note, ed_type exists for earlier years, drop before creating new variable)
rename ed_type old_ed_type
gen ed_type=.
replace ed_type = old_ed_type if (ref_year<1970)
replace ed_type=1 if (ref_year>=1972 & ref_year<=1973 & (educ_ref==1 | educ_ref==2 | educ_ref==6))
replace ed_type=2 if (ref_year>=1972 & ref_year<=1973 & educ_ref==3)
replace ed_type=3 if (ref_year>=1972 & ref_year<=1973 & educ_ref==4)
replace ed_type=4 if (ref_year>=1972 & ref_year<=1973 & educ_ref==5)
replace ed_type=1 if (qyear>=801 & qyear<=955 & (educ_ref==1 | educ_ref==2 | educ_ref==7))
replace ed_type=2 if (qyear>=801 & qyear<=955 & educ_ref==3)
replace ed_type=3 if (qyear>=801 & qyear<=955 & educ_ref==4)
replace ed_type=4 if (qyear>=801 & qyear<=955 & (educ_ref==5 | educ_ref==6))
replace ed_type=1 if (qyear>955 & (educ_ref==0 | educ_ref==10 | educ_ref==11))
replace ed_type=2 if (qyear>955 & educ_ref==12)
replace ed_type=3 if (qyear>955 & (educ_ref>=13 & educ_ref<=14))
replace ed_type=4 if (qyear>955 & (educ_ref>=15 & educ_ref<=17))
forvalues x=1/4 {
gen ed`x'=0
replace ed`x'=1 if ed_type==`x'
}
*Gen age indicators 
gen age_grp=.
replace age_grp=1 if (age_ref>=0 & age_ref<=34)
replace age_grp=2 if (age_ref>=35 & age_ref<=49)
replace age_grp=3 if (age_ref>=50 & age_ref<=64)
replace age_grp=4 if (age_ref>=65)
*Gen home owner dummy
gen ownhome=.
replace ownhome=1 if (cutenure>=1 & cutenure<=3)
replace ownhome=0 if (cutenure>=4 & cutenure<=6)
gen region_code=region
replace region_code=5 if (bls_urbn==2) 
*Gen demographic groups 
gen dem_group2=10*ftype+emp_type
gen dem_group3=10*ftype+race_type
gen dem_group4=10*ftype+region_code
gen dem_group6=100*ftype+10*ed_type+race1
*Change dem_group6 into continuous variable 1/40
egen counter=group(dem_group6)
quietly {
forvalues x=1/40 {
gen grp_dum`x'=0
replace grp_dum`x'=1 if counter==`x'
}
}


keep newid ref_year qyear scale r_cons* fincbtax cpi_u_rs_adj fam_size perslt18 earncomp occ_recode age_grp ed_type race1 male topcode_inc respstat 

save inequality_for_elasticity , replace 

use inequality_data_for_80 , clear 
drop if (ref_year==1983 | ref_year==1984)

keep newid ref_year qyear scale r_cons* fincbtax cpi_u_rs_adj fam_size perslt18 earncomp occ_recode age_grp ed_type race1 male topcode_inc respstat 

append using inequality_for_elasticity 

save inequality_for_elasticity , replace 

do elasticities_7.do 
